package com.door.erp.sevice.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.door.erp.dao.ExpenditureDao;
import com.door.erp.dao.base.GenericDao;
import com.door.erp.model.Expenditure;
import com.door.erp.sevice.ExpenditureService;
import com.door.erp.sevice.base.GenericServiceImpl;
import com.mysql.jdbc.StringUtils;

/**
 *类说明
 *
 * @author Xuelin Yang E-mail:yangxuelin@foxmail.com
 * @version 创建时间：2013-6-3 上午9:51:03
 */
@Service(value="expenditureService")
public class ExpenditureServiceImpl extends
		GenericServiceImpl<Expenditure, Long> implements ExpenditureService {

	@Resource(name="expenditureDao", type=ExpenditureDao.class)
	private ExpenditureDao expenditureDao;
	
	@Override
	public GenericDao<Expenditure, Long> getDao() {
		return this.expenditureDao;
	}
	
	public List<Expenditure> getExpenditureByTimeAndType(String beginTimeStr, String endTimeStr, String typeStr, String employeeStr) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date beginTime = null;
		Date endTime = null;
		Long employeeId = null;
		Long typeId = null;
		
		if(!StringUtils.isNullOrEmpty(beginTimeStr)) {
			try {
				beginTime = sdf.parse(beginTimeStr);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		}
		
		if(!StringUtils.isNullOrEmpty(endTimeStr)) {
			try {
				endTime = sdf.parse(endTimeStr);
				endTime =  new Date(endTime.getTime() + 24 * 60 * 60 * 1000);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		}
		if(!StringUtils.isNullOrEmpty(employeeStr)) {
			employeeId = Long.parseLong(employeeStr);
		}
		if(!StringUtils.isNullOrEmpty(typeStr)) {
			typeId = Long.parseLong(typeStr);
		}
		return expenditureDao.getExpenditureByTimeAndType(beginTime, endTime, typeId, employeeId);
	}

}
